#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        // child
        int cnt = 5;
        while (cnt)
        {
            cout << "I am a process " << getpid() << endl;
            sleep(1);
            cnt--;
        }
        exit(0);
    }
    // farther
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(id == ret)
    {
        cout << "wait success " << "exit code: " << ((status>>8)&0xFF) << "signal code: " << (status&0x7f) 
        << "core dump: " << ((status>>7)&1) << endl; 
    }

    return 0;
}